<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/12/25
 * Time: 16:18
 */

namespace app\admin\controller;

use app\admin\model\DinosaurDetail;
use app\admin\model\FightDetail;
use app\admin\model\MemberEarningDetail;
use app\admin\model\TransactionDetail;
use app\common\CacheKey;
use think\Db;

class Dinosaur extends AdminBase
{
    public function getList()
    {
        $data = DinosaurDetail::getInstance()->getList();

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("dinosaur", config('app.dinosaur'));
        $this->assign("keywords", input('param.keywords', ''));
        $this->assign("range", input('param.range', ''));
        $this->assign("dinosaur_type", input('param.dinosaur_type'));

        return $this->fetch();
    }

    /**
     * @Title: export
     * @Describe 导出到excel
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @author lijunwei
     * @Date 2022/1/6 15:55
     */
    public function export()
    {
        $lists = DinosaurDetail::getInstance()->exportExcel();
        $result = [];
        foreach ($lists as $key => $list) {
            if ($list['fight_status'] == 1) {
                $fight_status = '休息中';
            } else {
                $fight_status = '战斗中';
            }
            if ($list['trans_status'] == 1) {
                $trans_status = '待出售';
            } else {
                $trans_status = '出售中';
            }
            $result[$key] = [
                'id' => $list['id'],
                'add_time' => $list['add_time'],
                'dinosaur_name' => $list['dinosaur_name'] . '(' . $list['dinosaur_level_id'] . ')',
                'nft_id' => $list['nft_id'],
                'owner_address' => $list['owner_address'],
                'fight_status' => $fight_status,
                'trans_status' => $trans_status,
            ];
        }

        $header = [
            'A' => [
                'title' => '恐龙ID',
                'value' => 'id',
            ],
            'B' => [
                'title' => '创建时间',
                'value' => 'add_time',
            ],
            'C' => [
                'title' => '恐龙名称(级别)',
                'value' => 'dinosaur_name',
            ],
            'D' => [
                'title' => '恐龙NFT ID',
                'value' => 'nft_id',
            ],
            'E' => [
                'title' => '所有者地址',
                'value' => 'owner_address',
            ],
            'F' => [
                'title' => '战斗状态',
                'value' => 'fight_status',
            ],
            'G' => [
                'title' => '交易状态',
                'value' => 'trans_status',
            ]
        ];
        $name = '恐龙明细';

        outExcel($result, $header, $name);
    }

    /**
     * @Title: fightLog
     * @Describe 战斗日志
     * @return \think\response\Json|\think\response\View
     * @author lijunwei
     * @Date 2022/1/5 18:50
     */
    public function fightLog()
    {
        $dinosaur_level_id = input('dinosaur_level_id');
        $userAddress = input('address');
        $nftId = input('nftid');
        $where = ['nft_id' => $nftId, 'dinosaur_level_id' => $dinosaur_level_id, 'user_address' => $userAddress];
        $data = FightDetail::getInstance()->getPage($where);

        return json(['code' => 1, 'msg' => '', 'data' => $data]);
    }

    /**
     * @Title: transLog
     * @Describe 交易日志
     * @return \think\response\Json|\think\response\View
     * @author lijunwei
     * @Date 2022/1/5 18:51
     */
    public function transLog()
    {
        $dinosaur_level_id = input('dinosaur_level_id');
//        $userAddress = input('address');
        $nftId = input('nftid');
        $where = ['nft_id' => $nftId, 'dinosaur_level_id' => $dinosaur_level_id];
        $data = TransactionDetail::getInstance()->getPage($where);

        return json(['code' => 1, 'msg' => '', 'data' => $data]);
    }
}